package com.zy.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

//堆
//31个元素
public class HeapView extends View {


    public HeapView(Context context) {
        super(context);
        init(context);
    }


    public HeapView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HeapView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public HeapView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    Paint nodePaint;
    Paint textPaint;
    Paint assistLinePaint;

    private void init(Context context) {
        nodePaint = new Paint();
        nodePaint.setColor(Color.WHITE);

        textPaint = new Paint();
        //设置文本大小
        textPaint.setTextSize(72);
        //设置文本对齐方式
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setColor(Color.RED);

        assistLinePaint = new Paint();
        assistLinePaint.setColor(Color.WHITE);
        DashPathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 10);
        assistLinePaint.setPathEffect(effects);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //Log.d("LineSetView",""+getWidth()+" "+getHeight());
        int height = getHeight();

        List<Integer> path = new ArrayList<>();
        path.add(0);
        drawHeapNode(canvas, true, path, 0, getWidth() / 2, 100);
    }


    int lineLength = 200;
    int baseLineWidth = 500;

    int radius = 50;
    List<Integer> assistLineList = new ArrayList<>();

    int maxDepth = 4;

    private void drawHeapNode(Canvas canvas, boolean root, List<Integer> path, int depth, int centerX, int centerY) {
        if (depth > maxDepth) {
            return;
        }

        int curLineWidth = (int) (baseLineWidth / (Math.pow(2, depth)));

        //圆
        //drawAssistLine(depth, canvas, centerX, centerY);
        //画辅助线
        if (depth < maxDepth) {
            canvas.drawLine(centerX, centerY, centerX - curLineWidth, centerY + lineLength, assistLinePaint);
            canvas.drawLine(centerX, centerY, centerX + curLineWidth, centerY + lineLength, assistLinePaint);
        }

        canvas.drawCircle(centerX, centerY, radius, nodePaint);
        drawTextCenter(canvas, centerX, centerY, "" + (getData(path)));

        Log.d("ZYDebug", "drawHeapNode:" + path);

        //左
        List<Integer> leftPath = new ArrayList<>();
        leftPath.addAll(path);
        leftPath.add(1);
        drawHeapNode(canvas, false, leftPath, depth + 1, centerX - curLineWidth, centerY + lineLength);
        //右

        List<Integer> rightPath = new ArrayList<>();
        rightPath.addAll(path);
        rightPath.add(2);
        drawHeapNode(canvas, false, rightPath, depth + 1, centerX + curLineWidth, centerY + lineLength);
    }

    private void drawAssistLine(int depth, Canvas canvas, int centerX, int centerY) {
        if (!assistLineList.contains(depth)) {
            assistLineList.add(depth);
            canvas.drawLine(0, centerY, getWidth(), centerY, assistLinePaint);
        }
    }


    private void drawTextCenter(Canvas canvas, int centerX, int centerY, String text) {
        //计算基线坐标
        float baseLineY = getBaseLineY(textPaint, centerY);
        canvas.drawText(text, centerX, baseLineY, textPaint);
    }


    private float getBaseLineY(Paint textPaint, float centerY) {
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        return centerY + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
    }


    int[] dataS;

    public void setData(int[] dataS, List<Integer> opIndexList) {
        this.dataS = dataS;
        invalidate();
    }

    private int getData(List<Integer> path) {
        //02222
        int index = 0;
        for (Integer p : path) {
            if (p == 1) {
                index = index * 2 + 1;
            } else if (p == 2) {
                index = index * 2 + 2;
            }
        }

        return dataS[index];
    }
}
